package com.dxtx.widget;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.TextView;

public class ShanView extends TextView {
    // private LinearGradient mLinearGradient;
    private Shader mGradient;
    private Matrix mGradientMatrix;
    private Paint mPaint;
    private int mViewWidth = 0, mViewHeight = 0;
    ;
    private float mTranslateX = 0, mTranslateY = 0;

    private boolean mAnimating = false;

    public ShanView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public ShanView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ShanView(Context context) {
        super(context);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int w = getMeasuredWidth();
        int h = getMeasuredHeight();
        rect = new Rect(0, 0, w, h);
    }

    private Rect rect;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mViewWidth == 0) {
            mViewWidth = getMeasuredWidth();
            mViewHeight = getMeasuredHeight();
            if (mViewWidth > 0) {
                mPaint = new Paint();
                //亮光闪过
                mGradient = new LinearGradient(0, 0, mViewWidth, mViewHeight, new int[]{0x00ffffff, 0x55ffffff,
                        0x00ffffff}, new float[]{0, 0.9f, 1}, Shader.TileMode.CLAMP);
                //暗色
                //  mGradient = new LinearGradient(0, 0, mViewWidth, mViewHeight, new int[]{0x00000000, 0x55000000,
                //  0x00000000}, new float[]{0, 0.9f, 1}, Shader.TileMode.CLAMP);
                //暗亮凸显(渲染慢,弃用)
                // mGradient = new LinearGradient(0, 0, mViewWidth, mViewHeight, new int[]{0x33000000, 0x00000000, 0x00ffffff, 0x88ffffff,
                //0x00ffffff, 0x00000000, 0x33000000}, new float[]{0, 0.3f, 0.3f, 0.58f, 0.7f, 0.7f, 1}, Shader.TileMode.CLAMP);
                mPaint.setShader(mGradient);
                mPaint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
                mGradientMatrix = new Matrix();
                mGradientMatrix.setTranslate(-2 * mViewWidth, mViewHeight);
                mGradient.setLocalMatrix(mGradientMatrix);
                rect = new Rect(0, 0, w, h);
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mAnimating && mGradientMatrix != null) {
            canvas.drawRect(rect, mPaint);
        }
    }

    private ValueAnimator valueAnimator;

    private void init() {
        initValueAnimator();
    }

    private void initValueAnimator() {
        valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.setDuration(6000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.addUpdateListener(new AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float v = (Float) animation.getAnimatedValue();
                mTranslateX = 4 * mViewWidth * v - mViewWidth * 2;
                mTranslateY = mViewHeight * v;
                mGradientMatrix.setTranslate(mTranslateX, mTranslateY);
                mGradient.setLocalMatrix(mGradientMatrix);
                invalidate();
            }
        });
        getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeGlobalOnLayoutListener(this);
                mAnimating = true;
                valueAnimator.start();
            }
        });
    }
}
